package mm.chensj.demo.realm;

import org.apache.shiro.authc.*;
import org.apache.shiro.realm.Realm;

/**
 * @author Lenovo
 * @title
 * @project shiro-demo
 * @package mm.chensj.demo.realm
 * @date: 2019-01-17 17:02
 */
public class DemoRealm implements Realm {
    @Override
    public String getName() {
        return getName();
    }

    @Override
    public boolean supports(AuthenticationToken authenticationToken) {
        return authenticationToken instanceof UsernamePasswordToken;
    }

    @Override
    public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        // 获取用户名
        String username = (String) authenticationToken.getPrincipal();
        // 获取密码
        String password = new String((char[])authenticationToken.getCredentials());
        if(!"zhang".equals(username)) {
            // 如果用户名错误
            throw new UnknownAccountException("用户名不存在");
        }
        if(!"123".equals(password)) {
            // 如果密码错误
            throw new IncorrectCredentialsException("用户名或密码错误");
        }
        // 如果身份认证验证成功，返回一个AuthenticationInfo实现；
        return new SimpleAuthenticationInfo(username, password, getName());
    }
}
